In modern computing, multithreading refers to concurrent management of multiple execution threads. The execution threads may be associated with a single software application, multiple software applications, or any combination thereof.
In a single-processor, single-core environment, only a single execution thread can be executed at any given time. However, a given execution thread may encounter a delay during which no execution is possible. For example, the execution thread may be awaiting user input, a network packet, storage device spin-up, etc. During such delays, another execution thread may be executed. In this manner, improved utilization of available processing resources is achieved.
Further, in a multi-processor or multi-core environment, multiple execution threads may be executed simultaneously. Simultaneous execution of multiple execution threads is referred to as parallel processing. Parallel processing generally allows multiple execution threads to be executed in a shorter period of time. However, the multiple execution threads may require access to the same system resource(s), such as memory, storage devices, network bandwidth, etc. In particular, in some cases, multiple execution threads may require access to the same data element (i.e., a data element stored in memory, persistent storage, etc.) at the same time.
If multiple execution threads are allowed to access a data element simultaneously, data inconsistency may result. For example, if one execution thread reads the data element while another execution thread is modifying the data element, the data that is read may be incomplete or corrupted. To avoid data inconsistency, lock mechanisms (e.g., semaphores, spinlocks, etc.) are typically used to allow only a single execution thread to access a data element at once. Specifically, a lock is obtained when an execution thread accesses a data element, and other execution threads are not allowed to read or modify the data element until the lock is released.